package com.fenghongzhang.day001;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TimePicker;
import android.widget.Toast;

import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {


    private int t= 0;

    private static final String TAG = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    /**
     * 普通对话框
     * @param view
     */
    public void click(View view) {
        /**
         * 构建对话框
         */
        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        builder.setIcon(R.mipmap.ic_launcher);
        builder.setTitle("标题")
                .setMessage("内容");


        builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "ok", Toast.LENGTH_SHORT).show();
            }
        });

        builder.setNegativeButton("bu", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "不ok", Toast.LENGTH_SHORT).show();
            }
        });

        AlertDialog alertDialog = builder.create();

        //显示
        alertDialog.show();
    }

    /**
     * 单选
     * @param view
     */
    public void click2(View view) {
        /**
         * 构建对话框
         */
        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        builder.setIcon(R.mipmap.ic_launcher);
        builder.setTitle("标题");



        builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "ok", Toast.LENGTH_SHORT).show();
            }
        });

        builder.setNegativeButton("bu", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "不ok", Toast.LENGTH_SHORT).show();
            }
        });

        //1.7
        final String[] s = new String[]{"男","女","不男不女"};

        /**
         * 数组
         * 默认选中的下标
         * 点击事件
         */
        builder.setSingleChoiceItems(s, 0, new DialogInterface.OnClickListener() {
            /**
             *
             * @param dialog 对话框
             * @param which  下标
             */
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, s[which], Toast.LENGTH_SHORT).show();
            }
        });

        AlertDialog alertDialog = builder.create();

        //显示
        alertDialog.show();
    }

    //多选
    public void click3(View view) {
        /**
         * 构建对话框
         */
        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        builder.setIcon(R.mipmap.ic_launcher);
        builder.setTitle("标题");

        final String[] str = new String[]{"游戏","睡觉","学习"};
        final boolean[] booleans = new boolean[]{true,false,true};
        builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "ok", Toast.LENGTH_SHORT).show();
                for (int i = 0; i <booleans.length; i++) {
                    if(booleans[i]){
                        Toast.makeText(MainActivity.this, str[i], Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });

        builder.setNegativeButton("bu", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "不ok", Toast.LENGTH_SHORT).show();
            }
        });



        //多选
        builder.setMultiChoiceItems(str, booleans, new DialogInterface.OnMultiChoiceClickListener() {
            /**
             *
             * @param dialog
             * @param which  数组下标
             * @param isChecked 是否被选中
             */
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                booleans[which] = isChecked;
            }
        });


        AlertDialog alertDialog = builder.create();

        //显示
        alertDialog.show();
    }

    //自定义内容
    public void click4(View view) {
        /**
         * 构建对话框
         */
        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        builder.setIcon(R.mipmap.ic_launcher);
        builder.setTitle("标题");




        builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "ok", Toast.LENGTH_SHORT).show();
            }
        });

        builder.setNegativeButton("bu", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "不ok", Toast.LENGTH_SHORT).show();
            }
        });


        View inflate = LayoutInflater.from(this).inflate(R.layout.diy_layout, null);
        builder.setView(inflate);
        //xml -> view
        final AlertDialog alertDialog = builder.create();

        ImageView imageView = inflate.findViewById(R.id.img);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
            }
        });
        //自定义内容

        //显示
        alertDialog.show();

    }

    //进度
    public void click5(View view) {
        /**
         * 1,创建对象
         * 2,设置属性
         * 3,显示
         * 4,更改进度
         */
       final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setMax(100);
        progressDialog.setProgress(0);
        //进度
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.show();
        //定时器
        final Timer timer = new Timer();
        //调用
        /**
         * 1,TimerTask
         * 2,延时,第一次执行的时间.
         * 3,间隔多久  毫秒
         */
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                t+=10;
                progressDialog.setProgress(t);
                if(t >= 100){
                    timer.cancel();
                    progressDialog.dismiss();
                }
            }
        },0,1000);


    }

    //日期
    public void click6(View view) {

        //日期类
        Calendar instance = Calendar.getInstance();

        DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                Toast.makeText(MainActivity.this, "年:"+year+"月:"+(month+1)+"日:"+dayOfMonth, Toast.LENGTH_SHORT).show();
            }
        }, instance.get(Calendar.YEAR), instance.get(Calendar.MONTH), instance.get(Calendar.DAY_OF_MONTH));

        datePickerDialog.show();
    }

    //时间
    public void click7(View view) {
        Calendar instance = Calendar.getInstance();

        TimePickerDialog timePickerDialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                Toast.makeText(MainActivity.this, hourOfDay+"-"+minute, Toast.LENGTH_SHORT).show();
            }
        }, instance.get(Calendar.HOUR), instance.get(Calendar.MINUTE), true);

        timePickerDialog.show();
    }
}
